//
//  Top_Widget.swift
//  Top Widget
//
//  Created by careate-hubing on 2021/12/13.
//  Copyright © 2021 huhu. All rights reserved.
//

import WidgetKit
import SwiftUI
import Intents

struct Provider: IntentTimelineProvider {
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), configuration: ConfigurationIntent())
    }

    func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date(), configuration: configuration)
        completion(entry)
    }

    func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        var entries: [SimpleEntry] = []

        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date()
        for hourOffset in 0 ..< 5 {
            let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
            let entry = SimpleEntry(date: entryDate, configuration: configuration)
            entries.append(entry)
        }

        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }
}

struct SimpleEntry: TimelineEntry {
    let date: Date
    let configuration: ConfigurationIntent
}

struct Top_WidgetEntryView : View {
    var entry: Provider.Entry
    @Environment(\.widgetFamily) var family
    
    @ViewBuilder
    var body :some View{
        switch family{
        case .systemSmall:
            //小尺寸
            Image(uiImage: UIImage.init(named: "评分星星")! )
                .resizable()
                .padding(.all)
                .frame(width: 50, height: 50, alignment:.center)
                .scaledToFit()
                .edgesIgnoringSafeArea(.all)
                .aspectRatio(contentMode: .fill)
                .clipShape(Circle())
                .overlay(Circle().stroke(Color.yellow,lineWidth: 4))
                .shadow(radius: 50/2)
            VStack (alignment:.leading){
                Text(entry.date,style: .time)
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundColor(.orange)
                .padding(.all)
                HStack {
                    Text(entry.date,style: .date)
                        .font(.headline)
                        .fontWeight(.medium)
                        .foregroundColor(Color.purple)
//                    Spacer()
                    Text("hello world")
                        .font(.subheadline)
                }
                .padding(5)
            }
        case .systemMedium:
            // 中尺寸
            Image(uiImage: UIImage.init(named: "评分星星")! )
                .resizable()
                .frame(width: 200, height: 100, alignment:.center)
                .scaledToFit()
                .edgesIgnoringSafeArea(.all)
                .aspectRatio(contentMode: .fill)
            Text(entry.date,style: .time)
        default:
            //大尺寸
            Image(uiImage: UIImage.init(named: "评分星星")! )
                .resizable()
                .frame(width: 200, height: 100, alignment:.center)
                .scaledToFit()
                .edgesIgnoringSafeArea(.all)
                .aspectRatio(contentMode: .fill)
            Text(entry.date,style: .time)
        }
    }
}

@main
struct TopSwift_Widget: WidgetBundle{
    @WidgetBundleBuilder
    var body: some Widget{
        Top_Widget();
    }
}


struct Top_Widget: Widget {
    let kind: String = "Top_Widget"

    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
            Top_WidgetEntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
    //    .supportedFamilies([.systemSmall,.systemMedium])
    }
}

struct Top_Widget_Previews: PreviewProvider {
    static var previews: some View {
        Top_WidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
            .previewContext(WidgetPreviewContext(family: .systemSmall))
    }
}
